package course13;

import java.util.ArrayList;
import java.util.List;

public class WorldTime {
    public static void main(String[] args) {
        final long timeInterval = 1000;
        Runnable run1 = new Runnable() {
            IAnimal animal = new Fox();
            public void run() {
                //匿名类方式
                //用来保存动物的接口数据，里面存放了动物的接口
                List<IAnimal> animals = new ArrayList<IAnimal>();
                animals.add(new Worm());
                animals.add(new Worm());
                animals.add(new Worm());
                animals.add(new Worm());
                while (true)//保证世界不会停止，一直运行
                {//循环数组中的每一个动物
                    for (int i = 0; i < animals.size(); i++) {
                        animals.get(i).grow();
                    }
                    try {
                        Thread.sleep(timeInterval);
                    } catch (InterruptedException e) {
                        System.out.println("异常了！");
                    }

                }//while 方法结束
            }//run（）方法结束
        };//Runnable方法结束
        Thread t1 = new Thread(run1); //创建线程
        t1.start();
    }
//    IAnimal animal = new Fox();
//    IAnimal iAnimal;
//    int birthNum = (int)(Math.random()*4);
//    switch(birthNum){
//        case 0:
//            iAnimal = new Worm();
//            break;
//        case 1:
//            iAnimal = new Fox();
//            break;
//        case 2:
//            iAnimal = new Rabbit();
//            break;
//        default:
//            iAnimal = new Worm();
//    }
//    animals.add(iAnimal);


}
